<template>
  <el-row class="login-container">
    <!-- 响应式布局：
      :lg="16" 表示在大屏（PC）中显示左边占据16/24，右边8/24
      :md="12" 表示在平板中显示为左右各占一半
      没有显式的设置手机端显示样式，则会将左右两部分转换为上下显示 - 左右两部分都占用24，即显示为两行
     -->
    <el-col :lg="16" :md="12" class="left">
      <div>
        <!-- class="font-bold text-5xl text-light-50 mb-4" -->
        <div>欢迎光临</div>
        <!-- class="text-gray-200 text-sm" -->
        <div>此站点是《vue3+vite实战商城后台开发》视频课程的演示项目</div>
      </div>
    </el-col>

    <!-- 右边布局 -->
    <el-col :lg="8" :md="12" class="right">
      <h2 class="title">欢迎回来</h2>
      <!-- class="flex items-center justify-center my-5 text-gray-300 space-x-2" -->
      <div>
        <span class="line"></span>
        <span>账号密码登录</span>
        <span class="line"></span>
      </div>

      <!-- 登录表单 -->
      <el-form ref="formRef" :model="form" class="w-[250px]" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><user /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" type="password" show-password>
            <template #prefix>
              <el-icon><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="w-[250px]" type="primary" @click="onSubmit" round color="#626aef" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive, onMounted, onBeforeMount } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { useToast } from 'vue-toastification'

const toast = useToast();
const store = useStore();
const router = useRouter();
const formRef = ref(null);
const form = reactive({
  username: "",
  password: "",
});

const rules = {
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
    {
      min: 3,
      max: 5,
      message: "用户名长度要在 3-5 个字符",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
  ],
};

const loading = ref(false);
const onSubmit = () => {
  formRef.value.validate((valid) => {
    if (!valid) return;
    loading.value = true;

    // 使用 store.login 替换下面登录方法
    store
      .dispatch("login", form)
      .then((res) => {
        // successMsg("登录成功");
        toast.success('登录成功')
        router.push("/"); // 跳转到主页
      })
      .finally(() => {
        loading.value = false;
      });
  });
};

function onKeyUp(e) {
  if (e.key == "Enter") onSubmit();
}

onMounted(() => {
  document.addEventListener("keyup", onKeyUp);
});

onBeforeMount(() => {
  document.removeEventListener("keyup", onKeyUp);
});
</script>

<style>
.login-container {
  @apply bg-indigo-500 min-h-screen;
}

.login-container .left {
  @apply flex items-center justify-center;
}

.login-container .right {
  @apply bg-indigo-50 flex flex-col items-center justify-center;
}

.left > div > div:first-child {
  @apply font-bold text-5xl text-light-50 mb-4;
}

.left > div > div:last-child {
  @apply text-gray-200 text-sm;
}

.right .title {
  @apply font-bold text-3xl text-gray-800;
}

.right > div {
  @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
}

.line {
  @apply h-[1px] w-16 bg-gray-200;
}
</style>
